package com.yanze.service.impl;

import com.github.pagehelper.PageHelper;
import com.yanze.mapper.LostedimageMapper;
import com.yanze.mapper.LostedthingMapper;
import com.yanze.pojo.Lostedimage;
import com.yanze.pojo.Lostedthing;
import com.yanze.service.LostedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class LostedServiceimpl implements LostedService {

    @Autowired
    LostedthingMapper lostedthingMapper;

    @Autowired
    LostedimageMapper lostedimageMapper;


    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int publishLosted(Lostedthing thing) {
        int flag = lostedthingMapper.insert(thing);
        return flag;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Lostedthing> getAllLosted(int page, int pageSize) {
        PageHelper.startPage(page, pageSize);
        Example example = new Example(Lostedthing.class);
        example.orderBy("time").desc();//按发表时间排序
        List<Lostedthing> things = lostedthingMapper.selectByExample(example);
        return things;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Lostedthing> getLostedById(String userid, int page, int pageSize) {
        PageHelper.startPage(page, pageSize);
        Example example = new Example(Lostedthing.class);
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmptyOrWhitespace(userid)) {
            criteria.andLike("userid", "%" + userid + "%");
        }
        example.orderBy("time").desc();//按发表时间排序
        List<Lostedthing> things = lostedthingMapper.selectByExample(example);
        return things;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int deleteLosted(Long lostedid) {
        int flag = lostedthingMapper.deleteByPrimaryKey(lostedid);
        return flag;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int insertImage(Lostedimage image) {
        int flag = lostedimageMapper.insert(image);
        return flag;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int insertImages(List<Lostedimage> images) {
        int flag = lostedimageMapper.insertList(images);
        return flag;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Lostedimage> queryImageByid(Long id) {
        Example example = new Example(Lostedthing.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLike("lostedid", "%" + id + "%");
        example.orderBy("time").desc();//按发表时间排序
        List<Lostedimage> images = lostedimageMapper.selectByExample(example);
        return images;
    }
}
